import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from '@/components/ui/card';
import { useToast } from '@/hooks/use-toast';
import { contactAPI, type ContactFormData } from '@/lib/api';
import { Mail, Phone, MapPin, Send } from 'lucide-react';

export function ContactPage() {
  const { t } = useTranslation();
  const { toast } = useToast();
  const [isLoading, setIsLoading] = useState(false);
  const [formData, setFormData] = useState<ContactFormData>({
    name: '',
    email: '',
    message: '',
  });

  const handleInputChange = (
    e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
  ) => {
    const { name, value } = e.target;
    setFormData(prev => ({ ...prev, [name]: value }));
  };

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    setIsLoading(true);

    try {
      await contactAPI.submit(formData);
      toast({
        title: t('contact.form.success'),
        description: 'We will get back to you soon.',
      });
      setFormData({ name: '', email: '', message: '' });
    } catch (error) {
      toast({
        title: t('contact.form.error'),
        description: 'Please try again later.',
        variant: 'destructive',
      });
    } finally {
      setIsLoading(false);
    }
  };

  const contactInfo = [
    {
      icon: Mail,
      title: 'Email',
      content: 'contact@example.com',
      description: 'Send us an email anytime',
    },
    {
      icon: Phone,
      title: 'Phone',
      content: '+1 (555) 123-4567',
      description: 'Mon-Fri from 8am to 5pm',
    },
    {
      icon: MapPin,
      title: 'Office',
      content: '123 Business St, City, State 12345',
      description: 'Come say hello at our office',
    },
  ];

  return (
    <div className='space-y-12'>
      {/* Header */}
      <div className='text-center space-y-4'>
        <h1 className='text-4xl font-bold tracking-tighter sm:text-5xl'>
          {t('contact.title')}
        </h1>
        <p className='mx-auto max-w-[700px] text-lg text-muted-foreground'>
          {t('contact.description')}
        </p>
      </div>

      <div className='grid gap-8 lg:grid-cols-2'>
        {/* Contact Form */}
        <Card>
          <CardHeader>
            <CardTitle>Send us a message</CardTitle>
            <CardDescription>
              Fill out the form below and we'll get back to you as soon as
              possible.
            </CardDescription>
          </CardHeader>
          <CardContent>
            <form onSubmit={handleSubmit} className='space-y-6'>
              <div className='space-y-2'>
                <label htmlFor='name' className='text-sm font-medium'>
                  {t('contact.form.name')}
                </label>
                <Input
                  id='name'
                  name='name'
                  type='text'
                  required
                  value={formData.name}
                  onChange={handleInputChange}
                  placeholder='Your full name'
                />
              </div>

              <div className='space-y-2'>
                <label htmlFor='email' className='text-sm font-medium'>
                  {t('contact.form.email')}
                </label>
                <Input
                  id='email'
                  name='email'
                  type='email'
                  required
                  value={formData.email}
                  onChange={handleInputChange}
                  placeholder='your.email@example.com'
                />
              </div>

              <div className='space-y-2'>
                <label htmlFor='message' className='text-sm font-medium'>
                  {t('contact.form.message')}
                </label>
                <Textarea
                  id='message'
                  name='message'
                  required
                  value={formData.message}
                  onChange={handleInputChange}
                  placeholder='Tell us about your project or question...'
                  rows={5}
                />
              </div>

              <Button type='submit' className='w-full' disabled={isLoading}>
                {isLoading ? (
                  <>
                    <div className='mr-2 h-4 w-4 animate-spin rounded-full border-2 border-current border-t-transparent' />
                    Sending...
                  </>
                ) : (
                  <>
                    <Send className='mr-2 h-4 w-4' />
                    {t('contact.form.submit')}
                  </>
                )}
              </Button>
            </form>
          </CardContent>
        </Card>

        {/* Contact Information */}
        <div className='space-y-6'>
          <div>
            <h2 className='text-2xl font-bold mb-4'>Get in touch</h2>
            <p className='text-muted-foreground'>
              We'd love to hear from you. Choose the best way to reach out to
              us.
            </p>
          </div>

          <div className='space-y-4'>
            {contactInfo.map((item, index) => {
              const Icon = item.icon;
              return (
                <Card key={index}>
                  <CardContent className='flex items-start space-x-4 p-6'>
                    <div className='w-10 h-10 rounded-lg bg-primary/10 flex items-center justify-center'>
                      <Icon className='h-5 w-5 text-primary' />
                    </div>
                    <div className='space-y-1'>
                      <h3 className='font-semibold'>{item.title}</h3>
                      <p className='font-medium'>{item.content}</p>
                      <p className='text-sm text-muted-foreground'>
                        {item.description}
                      </p>
                    </div>
                  </CardContent>
                </Card>
              );
            })}
          </div>
        </div>
      </div>
    </div>
  );
}
